from config import *
import logging
from logging import handlers

# 初始化logging
def init_log():
    # class init
    logger = logging.getLogger()
    # log level
    logger.setLevel(logging.INFO)
    # cmd showing
    sh = logging.StreamHandler()
    log_file = BASE_PATH + '/log/ego.log'
    # file log
    fh = handlers.TimedRotatingFileHandler(
        filename=log_file,
        when='D',  # days
        interval=1,  # how often
        backupCount=7,  # the log file save time
        encoding='utf-8'
    )
    # 定义格式
    # 发送时间 日志等级 用户名 文件名 方法名 出错行数 具体信息
    fmt = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
    formatter = logging.Formatter(fmt)

    # add fmt
    sh.setFormatter(formatter);  fh.setFormatter(formatter)

    # add logger
    logger.addHandler(sh);  logger.addHandler(fh)
